package com.luther.design.singleton;

import android.text.TextUtils;
import android.util.Log;

import java.util.HashMap;
import java.util.Map;

/**
 * 登记式
 * <h3>Description</h3>
 * TODO
 * <h3>Author</h3> luzhenbang
 * <h3>Date</h3> 03/04/2018 12:51
 * <h3>Copyright</h3> Copyright (c)2018 Shenzhen TL  Co., Ltd. Inc. All rights reserved.
 */
public class SingletonMap {
    /**设立静态变量，记录单例模式对象实例*/
    private static Map<String, Object> map = new HashMap<String, Object>();

    private SingletonMap  (){

    }

    public static Object getInstance(String name) {
        if (TextUtils.isEmpty(name)){
            name = SingletonMap.class.getName();
        }
        if (map.containsKey(name) && null != map.get(name)){
            return map.get(name);
        }else {
            try {
                map.put(name,Class.forName(name).newInstance());
            } catch (InstantiationException e) {
                Log.e(SingletonMap.class.getSimpleName() , e.getMessage());
            } catch (IllegalAccessException e) {
                Log.e(SingletonMap.class.getSimpleName() , e.getMessage());
            } catch (ClassNotFoundException e) {
                Log.e(SingletonMap.class.getSimpleName() , e.getMessage());
            }
            return map.get(name);
        }
    }

}
